﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using XChip_Common;
using DevExpress.XtraEditors;
using System.IO.Ports;
using SerialPortHelperLib;
using XChipTool.Enties;
using System.Threading;
using System.Configuration;

namespace XChipTool
{
    public partial class XChip_Reg_FCC : UserControl
    {
        public SerialPort sPortTemp;
        private bool isRead = false;
        private bool isWrite = false;
        List<string> listfiltration = new List<string> { "scale_sig", "slope", "offset" };
        private addressregisterManager aBll;
        private List<addressregister> XChipFcc;
        public XChip_Reg_FCC()
        {
            InitializeComponent();     
        }
        public XChip_Reg_FCC(SerialPort _sPortTemp, addressregisterManager _aBll)
        {
            sPortTemp = _sPortTemp;
            aBll = _aBll;
            XChipFcc = aBll.GetList(c => c.ChipType == ConfigurationManager.AppSettings["xc151_ChipType"] && (c.Classify == "FCC" ));
            InitializeComponent();
        }
        private void XChip_Reg_FCC_Load(object sender, EventArgs e)
        {
            Task.Run(() => {
            this.Invoke((EventHandler)delegate {
                for (int i = 0; i < XChipFcc.Count; i++)
                {

                    var type = AppHelper.NameTypeToString(XChipFcc[i].NameType);
                    //加上单选框的_en
                    if (XChipFcc[i].ControlType == "1")
                    {
                        type = XChipFcc[i].NameType;
                    }
                    var col = this.Controls.Find(type, true);


                    foreach (var item in col)
                    {
                        if (item is TextEdit)
                        {
                            //声明一个SuerToolTip
                            DevExpress.Utils.SuperToolTip graduationBatchSuperToolTip = new DevExpress.Utils.SuperToolTip();
                            //声明一个ToolTipTitleItem,保存标题
                            DevExpress.Utils.ToolTipTitleItem toolTipTitleItem = new DevExpress.Utils.ToolTipTitleItem();
                            String tooltips = "";

                            if (!string.IsNullOrEmpty(XChipFcc[i].Bit_Wide))
                            {
                                tooltips += $"位宽={XChipFcc[i].Bit_Wide}";
                            }
                            if (!string.IsNullOrEmpty(XChipFcc[i].Bit_Accuracy))
                            {
                                tooltips += $"|精度={XChipFcc[i].Bit_Accuracy}";
                            }
                            //设置标题文字
                            toolTipTitleItem.Text = tooltips;
                            //把标题添加到SuerToolTip
                            graduationBatchSuperToolTip.Items.Add(toolTipTitleItem);
                            //指定控件的SuperTip
                            ((TextEdit)item).SuperTip = graduationBatchSuperToolTip;
                            //正则判断
                            ((TextEdit)item).Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.RegEx;
                            ((TextEdit)item).Properties.Mask.EditMask = BitsHelp.GetBitsToMask(XChipFcc[i].Bits, XChipFcc[i].Bit_Wide, XChipFcc.Where(c => c.NameType.ToLower().Contains(type.ToLower()) && c.ControlType == "0").Select(c => c.Bits).ToList());
                        }
                    }
                }
            });
            });
        }

        private void simpleButton5_Click(object sender, EventArgs e)
        {
            if (!sPortTemp.IsOpen)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                return;
            }
            WaitBefore.Start("读取中", "正在加载中", 10);
            isRead = true;
            string nameType = string.Empty;
            for (int i = 0; i < XChipFcc.Count; i++)
            {

                var type = AppHelper.NameTypeToString(XChipFcc[i].NameType);
                //加上单选框的_en
                if (XChipFcc[i].ControlType == "1")
                {
                    type = XChipFcc[i].NameType;
                }
                try
                {
                    Control col = this.Controls.Find(type, true)[0];
                    if (nameType == type) continue;
                    nameType = type;
                    
                    if (col is TextEdit)
                    {
                        //过滤掉指定寄存器
                        if (listfiltration.Exists(c=>c==col.Name.ToString()))
                        {
                            continue;
                        }
                        var nameList = XChipFcc.Where(c => c.NameType.ToLower().Contains(type)).ToList();
                        long tempU = 0;
                        foreach (var nitem in nameList)
                        {
                            tempU += GetFCCRegister(nitem.NameType);
                        }
                        col.Text = tempU.ToString("X");
                    }
                    else if (col is CheckEdit)
                    {
                        (col as CheckEdit).Checked = (GetFCCRegister(type) == 0);
                    }
                }
                catch (Exception)
                {
                    continue;
                }
                
            }
            isRead = false;
            WaitBefore.Close();
        }
        private long GetFCCRegister(string nameType)
        {
            if (!sPortTemp.IsOpen)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                return -1;
            }

            if (XChipFcc.Exists(c => c.NameType.ToLower() == nameType.ToLower()))
            {
                var model = XChipFcc.Find(c => c.NameType.ToLower() == nameType.ToLower());
                var Val = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                if (model.ControlType.Contains("1"))
                {
                    return BitsHelp.GetBitsCheckedToBool(model.Bits, model.Hex, Val.ToString("X2"));
                }
                else
                {
                    return BitsHelp.GetBitsToUint(model.Bits, model.Hex, Val.ToString("X2"));
                }
            }
            else
            {
                DevExpress.XtraEditors.XtraMessageBox.Show($"未找到{nameType}"); return -1;
            }
           
        }

        private void SetFCCRegister(addressregister model, ulong add, bool isCheck = true)
        {
            if (!sPortTemp.IsOpen)//串口检测
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("串口未打开");
                return;
            }
            if (isRead) return; 

            SerialSend.WriteRegister(Convert.ToUInt32(model.Address, 16), add, sPortTemp);

        }

        private void keep_green_avg_en_CheckedChanged(object sender, EventArgs e)
        {
            if (isRead) return;
            if (XChipFcc.Exists(c => c.NameType.ToLower() == ($"keep_green_avg_en").ToLower()))
            {
                var model = XChipFcc.Find(c => c.NameType.ToLower() == ($"keep_green_avg_en").ToLower());
                var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), keep_green_avg_en.Checked);
                SetFCCRegister(model, Val);
            }
        }

        private void keep_green_iir_en_CheckedChanged(object sender, EventArgs e)
        {
            if (isRead) return;
            if (XChipFcc.Exists(c => c.NameType.ToLower() == ($"keep_green_iir_en").ToLower()))
            {
                var model = XChipFcc.Find(c => c.NameType.ToLower() == ($"keep_green_iir_en").ToLower());
                var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), keep_green_iir_en.Checked);
                SetFCCRegister(model, Val);
            }
        }

        private void keep_green_adj_en_CheckedChanged(object sender, EventArgs e)
        {
            if (isRead) return;
            if (XChipFcc.Exists(c => c.NameType.ToLower() == ($"keep_green_adj_en").ToLower()))
            {
                var model = XChipFcc.Find(c => c.NameType.ToLower() == ($"keep_green_adj_en").ToLower());
                var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), keep_green_adj_en.Checked);
                SetFCCRegister(model, Val);
            }
        }

        private void scale_sig_man_en_CheckedChanged(object sender, EventArgs e)
        {
            if (isRead) return;
            if (XChipFcc.Exists(c => c.NameType.ToLower() == ($"scale_sig_man_en").ToLower()))
            {
                var model = XChipFcc.Find(c => c.NameType.ToLower() == ($"scale_sig_man_en").ToLower());
                var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), scale_sig_man_en.Checked);
                SetFCCRegister(model, Val);
            }
        }

        private void keep_green_pxl_sat_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(keep_green_pxl_sat.Text)) return;
            if (XChipFcc.Exists(c => c.NameType.ToLower() == ($"keep_green_pxl_sat").ToLower()))
            {
                var model = XChipFcc.Find(c => c.NameType.ToLower() == ($"keep_green_pxl_sat").ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, keep_green_pxl_sat.Text);
                SetFCCRegister(model, Val);
            }
        }

        private void keep_green_iir_str_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(keep_green_iir_str.Text)) return;
            if (XChipFcc.Exists(c => c.NameType.ToLower() == ($"keep_green_iir_str").ToLower()))
            {
                var model = XChipFcc.Find(c => c.NameType.ToLower() == ($"keep_green_iir_str").ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, keep_green_pxl_sat.Text);
                SetFCCRegister(model, Val);
            }
        }

        private void fcc_str_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(fcc_str.Text)) return;
            if (XChipFcc.Exists(c => c.NameType.ToLower() == ($"fcc_str").ToLower()))
            {
                var model = XChipFcc.Find(c => c.NameType.ToLower() == ($"fcc_str").ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, fcc_str.Text);
                SetFCCRegister(model, Val);
            }
        }

        private void slope_k_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(fcc_str.Text)) return;
            if (XChipFcc.Exists(c => c.NameType.ToLower() == ($"slope_k").ToLower()))
            {
                var model = XChipFcc.Find(c => c.NameType.ToLower() == ($"slope_k").ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, slope_k.Text);
                SetFCCRegister(model, Val);
            }
        }

        private void offset_k_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(offset_k.Text)) return;
            if (XChipFcc.Exists(c => c.NameType.ToLower() == ($"offset_k").ToLower()))
            {
                var model = XChipFcc.Find(c => c.NameType.ToLower() == ($"offset_k").ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, offset_k.Text);
                SetFCCRegister(model, Val);
            }
        }

        private void slope_b_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(slope_b.Text)) return;
            if (XChipFcc.Exists(c => c.NameType.ToLower() == ($"slope_b").ToLower()))
            {
                var model = XChipFcc.Find(c => c.NameType.ToLower() == ($"slope_b").ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, slope_b.Text);
                SetFCCRegister(model, Val);
            }
        }

        private void offset_b_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(offset_b.Text)) return;
            if (XChipFcc.Exists(c => c.NameType.ToLower() == ($"offset_b").ToLower()))
            {
                var model = XChipFcc.Find(c => c.NameType.ToLower() == ($"offset_b").ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, offset_b.Text);
                SetFCCRegister(model, Val);
            }
        }

        private void sigma_sat_scale_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(sigma_sat_scale.Text)) return;
            if (XChipFcc.Exists(c => c.NameType.ToLower() == ($"sigma_sat_scale").ToLower()))
            {
                var model = XChipFcc.Find(c => c.NameType.ToLower() == ($"sigma_sat_scale").ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, sigma_sat_scale.Text);
                SetFCCRegister(model, Val);
            }
        }

        private void scale_sig_list0_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            //if (null != sPortTemp && sPortTemp.IsOpen)
            {
                if (isRead) return;
                var txt = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txt.Text)) return;
                if (XChipFcc.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                {
                    var model = XChipFcc.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetFCCRegister(model, Val);
                }
            }
        }

        private void pxl_std_number_node0_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            //if (null != sPortTemp && sPortTemp.IsOpen)
            {
                if (isRead) return;
                var txt = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txt.Text)) return;
                if (XChipFcc.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                {
                    var model = XChipFcc.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetFCCRegister(model, Val);
                }
            }
        }

        private void pxl_std_weight_node0_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            //if (null != sPortTemp && sPortTemp.IsOpen)
            {
                if (isRead) return;
                var txt = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txt.Text)) return;
                if (XChipFcc.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                {
                    var model = XChipFcc.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetFCCRegister(model, Val);
                }
            }
        }

        private void pxl_sat_number_node0_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            //if (null != sPortTemp && sPortTemp.IsOpen)
            {
                if (isRead) return;
                var txt = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txt.Text)) return;
                if (XChipFcc.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                {
                    var model = XChipFcc.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetFCCRegister(model, Val);
                }
            }
        }

        private void pxl_sat_weight_node0_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            //if (null != sPortTemp && sPortTemp.IsOpen)
            {
                if (isRead) return;
                var txt = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txt.Text)) return;
                if (XChipFcc.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                {
                    var model = XChipFcc.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetFCCRegister(model, Val);
                }
            }
        }

        private void iir_sat_number_node0_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            //if (null != sPortTemp && sPortTemp.IsOpen)
            {
                if (isRead) return;
                var txt = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txt.Text)) return;
                if (XChipFcc.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                {
                    var model = XChipFcc.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetFCCRegister(model, Val);
                }
            }
        }

        private void iir_sat_weight_node0_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            //if (null != sPortTemp && sPortTemp.IsOpen)
            {
                if (isRead) return;
                var txt = sender as DevExpress.XtraEditors.TextEdit;
                if (string.IsNullOrEmpty(txt.Text)) return;
                if (XChipFcc.Exists(c => c.NameType.ToLower() == txt.Name.ToLower()))
                {
                    var model = XChipFcc.Find(c => c.NameType.ToLower() == txt.Name.ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txt.Text);
                    SetFCCRegister(model, Val);
                }
            }
        }

        private void scale_sig_man_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(scale_sig_man.Text)) return;
            if (XChipFcc.Exists(c => c.NameType.ToLower() == $"scale_sig_man".ToLower()))
            {
                var model = XChipFcc.Find(c => c.NameType.ToLower() == $"scale_sig_man".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, scale_sig_man.Text);
                SetFCCRegister(model, Val);
            }
        }

        private void scale_sig_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(scale_sig.Text)) return;
            if (XChipFcc.Exists(c => c.NameType.ToLower() == $"scale_sig".ToLower()))
            {
                var model = XChipFcc.Find(c => c.NameType.ToLower() == $"scale_sig".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, scale_sig.Text);
                SetFCCRegister(model, Val);
            }
        }

        private void slope_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(slope.Text)) return;
            if (XChipFcc.Exists(c => c.NameType.ToLower() == $"slope".ToLower()))
            {
                var model = XChipFcc.Find(c => c.NameType.ToLower() == $"slope".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, slope.Text);
                SetFCCRegister(model, Val);
            }
        }

        private void offset_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(offset.Text)) return;
            if (XChipFcc.Exists(c => c.NameType.ToLower() == $"offset".ToLower()))
            {
                var model = XChipFcc.Find(c => c.NameType.ToLower() == $"offset".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, offset.Text);
                SetFCCRegister(model, Val);
            }
        }

        private void pxl_std_slope_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(pxl_std_slope.Text)) return;
            if (XChipFcc.Exists(c => c.NameType.ToLower() == $"pxl_std_slope_h".ToLower()))
            {
                var model = XChipFcc.Find(c => c.NameType.ToLower() == $"pxl_std_slope_h".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, pxl_std_slope.Text);
                SetFCCRegister(model, Val);
            }
            if (XChipFcc.Exists(c => c.NameType.ToLower() == $"pxl_std_slope_l".ToLower()))
            {
                var model = XChipFcc.Find(c => c.NameType.ToLower() == $"pxl_std_slope_l".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, pxl_std_slope.Text);
                SetFCCRegister(model, Val);
            }
        }

        private void pxl_sat_slope_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(pxl_sat_slope.Text)) return;
            if (XChipFcc.Exists(c => c.NameType.ToLower() == $"pxl_sat_slope_h".ToLower()))
            {
                var model = XChipFcc.Find(c => c.NameType.ToLower() == $"pxl_sat_slope_h".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, pxl_sat_slope.Text);
                SetFCCRegister(model, Val);
            }
            if (XChipFcc.Exists(c => c.NameType.ToLower() == $"pxl_sat_slope_l".ToLower()))
            {
                var model = XChipFcc.Find(c => c.NameType.ToLower() == $"pxl_sat_slope_l".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, pxl_sat_slope.Text);
                SetFCCRegister(model, Val);
            }
        }

        private void iir_sat_slope0_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(iir_sat_slope0.Text)) return;
            if (XChipFcc.Exists(c => c.NameType.ToLower() == $"iir_sat_slope0_h".ToLower()))
            {
                var model = XChipFcc.Find(c => c.NameType.ToLower() == $"iir_sat_slope0_h".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, iir_sat_slope0.Text);
                SetFCCRegister(model, Val);
            }
            if (XChipFcc.Exists(c => c.NameType.ToLower() == $"iir_sat_slope0_l".ToLower()))
            {
                var model = XChipFcc.Find(c => c.NameType.ToLower() == $"iir_sat_slope0_l".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, iir_sat_slope0.Text);
                SetFCCRegister(model, Val);
            }
        }

        private void iir_sat_slope1_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            if (isRead) return; //判断是否在读，读的时候不进行写           
            if (string.IsNullOrEmpty(iir_sat_slope1.Text)) return;
            if (XChipFcc.Exists(c => c.NameType.ToLower() == $"iir_sat_slope1_h".ToLower()))
            {
                var model = XChipFcc.Find(c => c.NameType.ToLower() == $"iir_sat_slope1_h".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, iir_sat_slope1.Text);
                SetFCCRegister(model, Val);
            }
            if (XChipFcc.Exists(c => c.NameType.ToLower() == $"iir_sat_slope1_l".ToLower()))
            {
                var model = XChipFcc.Find(c => c.NameType.ToLower() == $"iir_sat_slope1_l".ToLower());
                var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, iir_sat_slope1.Text);
                SetFCCRegister(model, Val);
            }
        }

        private void keep_green_pxl_sat_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                keep_green_pxl_sat_Leave(sender, null);
            }
        }

        private void keep_green_iir_str_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                keep_green_iir_str_Leave(sender, null);
            }
        }

        private void fcc_str_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                fcc_str_Leave(sender, null);
            }
        }

        private void slope_k_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                slope_k_Leave(sender, null);
            }
        }

        private void offset_k_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                offset_k_Leave(sender, null);
            }
        }

        private void slope_b_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                slope_b_Leave(sender, null);
            }
        }

        private void offset_b_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                offset_b_Leave(sender, null);
            }
        }

        private void sigma_sat_scale_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                sigma_sat_scale_Leave(sender, null);
            }
        }

        private void scale_sig_list0_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                scale_sig_list0_Leave(sender, null);
            }
        }

        private void pxl_std_number_node0_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                pxl_std_number_node0_Leave(sender, null);
            }
        }

        private void pxl_std_weight_node0_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                pxl_std_weight_node0_Leave(sender, null);
            }
        }

        private void pxl_sat_number_node0_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                pxl_sat_number_node0_Leave(sender, null);
            }
        }

        private void pxl_sat_weight_node0_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                pxl_sat_weight_node0_Leave(sender, null);
            }
        }

        private void iir_sat_number_node0_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                iir_sat_number_node0_Leave(sender, null);
            }
        }

        private void iir_sat_weight_node0_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                iir_sat_weight_node0_Leave(sender, null);
            }
        }

        private void scale_sig_man_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                scale_sig_man_Leave(sender, null);
            }
        }

        private void scale_sig_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                scale_sig_Leave(sender, null);
            }
        }

        private void slope_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                slope_Leave(sender, null);
            }
        }

        private void offset_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                offset_Leave(sender, null);
            }
        }

        private void pxl_std_slope_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                pxl_std_slope_Leave(sender, null);
            }
        }

        private void pxl_sat_slope_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                pxl_sat_slope_Leave(sender, null);
            }
        }

        private void iir_sat_slope0_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                iir_sat_slope0_Leave(sender, null);
            }
        }

        private void iir_sat_slope1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                iir_sat_slope1_Leave(sender, null);
            }
        }

        private void btn_SaveISPSetting_Click(object sender, EventArgs e)
        {
            if (!sPortTemp.IsOpen)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                return;
            }
            //SerialSend.isDebug = false;
            SaveFileDialog sfd = new SaveFileDialog();
            //设置文件类型 
            sfd.Filter = "配置文件（*.txt）|*.txt";

            //设置默认文件类型显示顺序 
            sfd.FilterIndex = 1;

            //保存对话框是否记忆上次打开的目录 
            sfd.RestoreDirectory = true;
            sfd.FileName = "IQ_Setting_FCC_" + DateTime.Now.ToString("yyyyMMddHHmms");
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                //StringBuilder sb = new StringBuilder();
                var Pe_dal = new parameter_exportManager();
                if (null != Pe_dal)
                {
                    WaitBefore.Start("参数导出", "正在加载中", 60);
                    var tempList = Pe_dal.GetListToExport().Where(c => c.Module == "FCC").OrderBy(c => c.SortNum).ToList();
                    #region 判断是否需要用白天的数据
                    var Pe_patch_dal = new parameter_export_patchManager();
                    var patchList = Pe_patch_dal.GetList();
                    var conditionList = tempList.Select(c => c.ConditionAddr).Distinct();
                    foreach (var item in conditionList)
                    {
                        if (SerialSend.ReadRegister(Convert.ToUInt32(item, 16), sPortTemp) == 0)
                        {
                            patchList.RemoveAll(c => c.ConditionAddr == item);
                        }
                    }
                    #endregion
                    var sb = XChipHelp.ReadParameterExportExpand(tempList, "80", patchList, sPortTemp);

                    System.IO.File.WriteAllText(sfd.FileName, sb.ToString());
                    WaitBefore.Close();
                    DevExpress.XtraEditors.XtraMessageBox.Show("参数导出完成");
                }
            }
        }

        private void simpleButton6_Click(object sender, EventArgs e)
        {
            //Thread thread = new Thread(threadPro);
            //thread.Start();
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            Get_SlopeOffsetSigmaNr();
        }

        public void Get_SlopeOffsetSigmaNr()
        {
            scale_sig.Text = GetFCCRegister("scale_sig").ToString("X2");
            slope.Text = GetFCCRegister("slope").ToString("X2");
            offset.Text = GetFCCRegister("offset").ToString("X2");
            pxl_std_slope.Text = (GetFCCRegister("pxl_std_slope_h") + GetFCCRegister("pxl_std_slope_l")).ToString("X4");
            pxl_sat_slope.Text = (GetFCCRegister("pxl_sat_slope_h") + GetFCCRegister("pxl_sat_slope_l")).ToString("X4");
            iir_sat_slope0.Text = (GetFCCRegister("iir_sat_slope0_h") + GetFCCRegister("iir_sat_slope0_l")).ToString("X4");
            iir_sat_slope1.Text = (GetFCCRegister("iir_sat_slope1_h") + GetFCCRegister("iir_sat_slope1_l")).ToString("X4");

            
        }
    }
}
